package com.jieshuibao.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.UserExMapper;
import com.jieshuibao.mapper.VideoBuyHistoryMapper;
import com.jieshuibao.mapper.VideoExMapper;
import com.jieshuibao.mapper.VideoMapper;
import com.jieshuibao.model.db.Video;
import com.jieshuibao.model.db.VideoBuyHistory;
import com.jieshuibao.model.db.VideoBuyHistoryExample;
import com.jieshuibao.model.db.VideoExample;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class VideoService{
	
	@Resource
	private VideoMapper videoMapper;
	
	@Resource
	private VideoExMapper videoExMapper;
	
	@Resource
	private UserExMapper userExMapper;
	
	@Resource
	private VideoBuyHistoryMapper videoBuyHistoryMapper;
	
	@Resource
	private UserDealRecordService userDealRecordService;
	
	public void addVideo(Video video) {
		videoMapper.insertSelective(video);
	}
	
	public void delVideoById(String id) {
		videoMapper.deleteByPrimaryKey(id);
	}
	
	public void updateVideo(Video video) {
		videoMapper.updateByPrimaryKeySelective(video);
	}
	
	public void updateVideoByExample(Video video,VideoExample videoExample){
	    videoMapper.updateByExampleSelective(video,videoExample);
	}
	
	public Video getVideoById(String id) {
		return videoMapper.selectByPrimaryKey(id);
	}
	
	public List<Video> getPageVideo(VideoExample videoExample) {
		return videoMapper.selectPage(videoExample);
	}
	
	public int countVideo(VideoExample videoExample) {
		return videoMapper.countByExample(videoExample);
	}
	
	public List<Video> selectByExample(VideoExample example){
	    return videoMapper.selectByExample(example);
	}
	/**
	 * 大咖访谈/根据标题查询视频/微课（单个）搜索
	 * @param map{"sinceId":1,"maxId":4,"type":"0","title":"微课堂","tax":"大咖访谈",
     * 				"topic":"大咖访谈","trade":"大咖访谈"}
	 * @return
	 */
	public List<Map<String, Object>> selectBySearchMap(Map<String, Object> map){
	    return videoExMapper.selectBySearchMap(map);
	}
	
	/**
	 * 视频是否购买过
	 * @param userId
	 * @param videoId
	 * @return
	 */
	public boolean isBuyVideo(String userId,String videoId){
		VideoBuyHistoryExample example = new VideoBuyHistoryExample();
		com.jieshuibao.model.db.VideoBuyHistoryExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andGoodsIdEqualTo(videoId);
		criteria.andStatusEqualTo(PayCommonUtil.PAYSUCCESS_STATUS);
		int i = videoBuyHistoryMapper.countByExample(example);
		if (i>0) {
			
			return true;
		}else {
			return false;
		}
		
		
		
	}
	
	
	//视频购买
	public boolean buyVideo(String userId,String productId,String payType,double money,String videoId){
		
		try {
			VideoBuyHistory videoBuyHistory = new VideoBuyHistory();
			videoBuyHistory.setCreateDate(new Date());
			videoBuyHistory.setGoodsId(videoId);
			videoBuyHistory.setGoodsType(PayTypeUtil.GOODSTYPE_VIDEO_SINGLE);
			//String uuid2 = UUIDGenerator.getUUID();
			videoBuyHistory.setId(productId);
			videoBuyHistory.setPrice(money);
			videoBuyHistory.setStatus(PayCommonUtil.PAYPENDING_STATUS);
			videoBuyHistory.setUserId(userId);
			if (payType.equals("balance")) {
				videoBuyHistory.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				videoBuyHistory.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				videoBuyHistoryMapper.insertSelective(videoBuyHistory);
				userDealRecordService.videoRunning(userId, productId, PayTypeUtil.PAYTYPE_BALANCE, money);
				return true;
			}else if (payType.equals("VipBalance")) {
				videoBuyHistory.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
				videoBuyHistory.setStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				videoBuyHistoryMapper.insertSelective(videoBuyHistory);
				userDealRecordService.videoRunning(userId, productId, PayTypeUtil.PAYTYPE_VIPBALANCE, money);
				return true;
			}else if (payType.equals("weixin")) {
				videoBuyHistory.setPayType(PayTypeUtil.PAYTYPE_WEIXIN);
				videoBuyHistory.setStatus(PayCommonUtil.PAYPENDING_STATUS);
				videoBuyHistoryMapper.insertSelective(videoBuyHistory);
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
			
	
		
	}
}
